﻿using System;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.AppLaunch.Common.Security;
using XuanFe.Database.BLL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class LoginForm : DevExpress.XtraEditors.XtraForm
    {
        private int times; // 登录错误次数
        public LoginForm()
        {
            InitializeComponent();

            this.times = 0;
        }
        private bool ValidityCheck()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (Global.Instance.MandatoryFieldEmpty(this.txtStaffId, false, ref message))
            {
                this.errorProvider1.SetError(this.txtStaffId, message);
                this.txtStaffId.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtPassword, false, ref message))
            {
                this.errorProvider1.SetError(this.txtPassword, message);
                this.txtPassword.Focus();
                return (false);
            }
            return (true);
        }
        private void LoginForm_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                Global.Instance.BeginWait();

                if (!this.ValidityCheck()) return;

                string staffId = this.txtStaffId.Text.Trim();
                string password = CryptographyHelper.Instance.MD5(string.Concat(staffId, this.txtPassword.Text.Trim()));
                LoginModel login = new LoginModel() { StaffId = staffId, Password = password, ComputerName = LoginInfo.Instance.ComputerName, IPAddress = LoginInfo.Instance.ComputerIP, MACAddress = LoginInfo.Instance.ComputerMAC };
                StaffLogonModel model;
                string msg = string.Empty;
                int r = StaffBLL.Instance.Login(login, out model);
                switch (r)
                {
                    case 0:
                        msg = "登录成功";
                        break;
                    case 1:
                        msg = "登录失败";
                        break;
                    case 2:
                        msg = "您的账号不存在";
                        break;
                    case 3:
                        msg = "您的密码不正确";
                        this.txtPassword.SelectAll();
                        break;
                    case 4:
                        msg = "您的账号尚未生效";
                        break;
                    case 5:
                        msg = "您已离职";
                        break;
                    case 6:
                        msg = "您的角色信息不存在";
                        break;
                    default:
                        msg = "未知错误，请联系管理员";
                        break;
                }
                if (r != 0)
                {
                    this.times++;
                    if (this.times >= 3)
                    {
                        Global.Instance.WarningDlg("您的输入错误次数达到三次，即将退出系统！");
                        this.DialogResult = DialogResult.Abort;
                    }
                    else
                    {
                        Global.Instance.ErrorDlg(msg);
                    }
                    return;
                }

                LoginInfo.Instance.StaffLogon = model;
                StaffLogBLL.Instance.InsertLog(staffId, string.Empty, string.Format("于工作站“{0}[{1}]”登录系统", LoginInfo.Instance.ComputerName, LoginInfo.Instance.ComputerIP));
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}
